﻿using Enough.Lifecycle.Common;
using Enough.Lifecycle.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Enough
{
    public class UpdatedEventArgs
    {
        public PackageVersion PreviousVersion { get; set; }
        public PackageVersion CurrentVersion { get; set; }
    }

    public class BaseApp : Application
    {
#if WINDOWS_PHONE_APP
        private TransitionCollection transitions;
#endif
        private string _lastLaunchArguments;



        /// <summary>
        /// Initializes the singleton instance of the <see cref="App"/> class. This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public BaseApp()
        {
            this.Suspending += this.OnSuspending;

        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used when the application is launched to open a specific file, to display
        /// search results, and so forth.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame != null && rootFrame.Content != null && ((Page)(rootFrame.Content)).DataContext is BaseViewModel)
            {
                ((BaseViewModel)((Page)(rootFrame.Content)).DataContext).OnAppActivated(e);
            }
        }

        protected override void OnActivated(IActivatedEventArgs e)
        {
            base.OnActivated(e);
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame.Content != null && ((Page)(rootFrame.Content)).DataContext is BaseViewModel)
            {
                ((BaseViewModel)((Page)(rootFrame.Content)).DataContext).OnAppActivated(e);
            }
        }


        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        internal virtual async void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            await SuspensionManager.SaveAsync();
            deferral.Complete();
        }


        public static void ClearNavigationBackStack()
        {
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame != null)
            {
                rootFrame.BackStack.Clear();
            }
        }

    }
}
